package org.steven.crm.core.service.area;

import org.steven.crm.core.service.BaseService;
import org.steven.crm.core.utils.TreeUtil;
import com.steven.xdb.dbmanager.DBManager;
import com.steven.xdb.pagination.query.AbstractQueryDataList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wuzw
 * @Date 2023/5/21 17:13
 * @Version 1.0
 */
public class AreaService extends BaseService {
    @Override
    protected String getTable() {
        return "area";
    }

    @Override
    protected AbstractQueryDataList getQueryDataList() {
        return null;
    }

    @Override
    protected String getDataSourceName() {
        return null;
    }

    public List<Map<String, Object>> makeTree(int maxLevel) throws Exception {
        List<Map<String, String>> areas = readAll(maxLevel, "id, name, pid");

        return TreeUtil.makeTree(areas, "pid");
    }

    public List<Map<String, String>> readAll(int maxLevel, String columns) throws Exception {
        String sql = "select " + columns + " from " + getTable() + " where level <= ?";
        List<String> p = new ArrayList<>();
        p.add(maxLevel + "");

        return DBManager.query(getSession(), sql, p);
    }

    public List<Map<String, Object>> readByPid(String pid, String columns, boolean withEmptyChild) throws Exception {
        String sql = "select " + columns + " from " + getTable() + " where pid = ?";
        List<String> p = new ArrayList<>();
        p.add(pid + "");

        List<Map<String, String>> datas = DBManager.query(getSession(), sql, p);
        List<Map<String, Object>> results = new ArrayList<>();
        for (Map<String, String> data : datas) {
            Map<String, Object> result = new HashMap<>();
            result.putAll(data);
            if (withEmptyChild) {
                result.put("children", new ArrayList<>());
            }

            results.add(result);
        }

        return results;
    }

    public List<Map<String, Object>> readAreaTreeById(String id, String columns) throws Exception {
        Map<String, String> area = this.read(id);
        String provinceId = area.get("sheng");
        String cityId = area.get("shi");

        List<Map<String, Object>> provinces = readByPid("0", columns, true);
        for (Map<String, Object> province : provinces) {
            if (province.get("id").equals(provinceId)) {
                List<Map<String, Object>> citys = readByPid(provinceId, columns, true);
                province.put("children", citys);
                for (Map<String, Object> city : citys) {
                    if (city.get("id").equals(cityId)) {
                        List<Map<String, Object>> towns = readByPid(cityId, columns, false);
                        city.put("children", towns);
                    }
                }
            }
        }

        return provinces;
    }

    public List<Map<String, String>> readProvinces() throws Exception {
        Map<String, String> k = new HashMap<>();
        k.put("level", "1");
        List<Map<String, String>> datas = DBManager.query(getSession(), getTable(), k);

        return datas;
    }

    public List<Map<String, String>> readCitys() throws Exception {
        Map<String, String> k = new HashMap<>();
        k.put("level", "2");
        List<Map<String, String>> datas = DBManager.query(getSession(), getTable(), k);

        return datas;
    }

    public List<Map<String, String>> readTowns() throws Exception {
        Map<String, String> k = new HashMap<>();
        k.put("level", "3");
        List<Map<String, String>> datas = DBManager.query(getSession(), getTable(), k);

        return datas;
    }
}
